<?php

namespace App\Listeners;

use App\Common\Constant\Params;
use App\Events\MasterAssureFund;
use App\Events\MasterOrderRefund;
use App\Events\MerchantRecharge;
use App\Events\OrderRefund;
use App\Models\MasterFund;
use App\Models\MasterTradeLog;
use App\Models\MerchantTradeLog;
use App\Services\IMerchantService;
use App\Services\ISystemService;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class MerchantRechargeListener implements ShouldQueue
{

    use InteractsWithQueue;

    public $queue = 'finance';

    public $tries = 3;

    public $timeout = 30;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  CacheHit  $event
     * @return void
     */
    public function handle(MerchantRecharge $event)
    {
        $trade_log = MerchantTradeLog::where(['type'=>Params::MERCHANT_TRADE_TYPE_RECHARGE,'id'=>$event->recharge_id])->first();
        if (!$trade_log){
            throw new \Exception('miss trade log');
        }

        $ret = resolve(IMerchantService::class)->genBalanceHistory($trade_log->id);
        if (!$ret){
            throw new \Exception('system balance raise fail');
        }
    }

    public function failed(MerchantRecharge $event, \Exception $exception)
    {
        //
    }
}
